package com.xinkao.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xinkao.example.dao.UserDao;
import com.xinkao.example.service.UserService;
import com.xinkao.example.util.CommonUtil;
import com.xinkao.example.util.constants.Constants;
import com.xinkao.example.util.constants.ErrorEnum;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: ZSX
 * @description: 用户ServiceImpl
 * @date: 2021/02/01
 */
@Service
public class UserServiceImpl implements UserService {

	@Resource
	private UserDao userDao;

	@Override
	public JSONObject addUser(JSONObject user , JSONObject jsonObject) {
		//登录用户验证
		if (null == user) {
			return CommonUtil.errorJson(ErrorEnum.E_20011);
		}
		//用户名验证
		int exist = userDao.verifyUser(jsonObject);
		if (exist > 0) {
			return CommonUtil.errorJson(ErrorEnum.E_10009);
		}
		String userId = UUID.randomUUID().toString().replaceAll("-","");
		SimpleHash simpleHashPassword = new SimpleHash(Constants.ENCRYPTION, Constants.DEFAULT_PASSWORD,null,Constants.TIMES);
		jsonObject.put("userId",userId);
		jsonObject.put("password",simpleHashPassword.toString());
		jsonObject.put("createBy",user.getString("userName"));
		userDao.addUser(jsonObject);
		return CommonUtil.successJson();
	}

	@Override
	public JSONObject updateUser(JSONObject user ,JSONObject jsonObject) {
		//登录用户验证
		if (null == user) {
			return CommonUtil.errorJson(ErrorEnum.E_20011);
		}
		//用户名验证
		String loginName = jsonObject.getString("loginName");
		String userId =jsonObject.getString("userId");
		JSONObject oldUserInfo = userDao.detailUser(userId);
		String oldName = oldUserInfo.getString("loginName");
		if (!oldName.equals(loginName)) {
			int exist = userDao.verifyUser(jsonObject);
			if (exist > 0) {
				return CommonUtil.errorJson(ErrorEnum.E_10009);
			}
		}
		jsonObject.put("updateBy",user.getString("userName"));
		userDao.updateUser(jsonObject);
		return CommonUtil.successJson();
	}

	@Override
	public JSONObject listAllUser(JSONObject jsonObject) {
		CommonUtil.fillPageParam(jsonObject);
		int count = userDao.countUser(jsonObject);
		List<JSONObject> infoList = userDao.listAllUser(jsonObject);
		return CommonUtil.successPage(jsonObject, infoList, count);
	}

	@Override
	public JSONObject deleteUser(JSONObject jsonObject) {
		String userId = jsonObject.getString("userId");
		userDao.deleteUser(userId);
		return CommonUtil.successJson();
	}

	@Override
	public JSONObject resetPassword(JSONObject jsonObject) {
		String userId = jsonObject.getString("userId");
		SimpleHash simpleHashPassword = new SimpleHash(Constants.ENCRYPTION, Constants.DEFAULT_PASSWORD,null,Constants.TIMES);
		userDao.updatePassword(userId,simpleHashPassword.toString());
		return CommonUtil.successJson();
	}

	@Override
	public JSONObject updatePassword(JSONObject user,JSONObject jsonObject) {
		//登录用户验证
		if (null == user) {
			return CommonUtil.errorJson(ErrorEnum.E_20011);
		}
		String userId = user.getString("userId");
		String oldPassword = jsonObject.getString("oldPassword");
		String newPassword = jsonObject.getString("newPassword");
		//获取数据库原数据
		JSONObject oldUserInfo = userDao.detailUser(userId);
		String dataBaseOldPassword = oldUserInfo.getString("password");
		//旧密码加密及对比
		SimpleHash simpleHashPasswordOld = new SimpleHash(Constants.ENCRYPTION, oldPassword,null,Constants.TIMES);
		if(!simpleHashPasswordOld.toString().equals(dataBaseOldPassword)){
			return CommonUtil.errorJson(ErrorEnum.E_10010);
		}
		//添加新密码
		SimpleHash simpleHashPassword = new SimpleHash(Constants.ENCRYPTION, newPassword,null,Constants.TIMES);
		userDao.updatePassword(userId,simpleHashPassword.toString());
		return CommonUtil.successJson();
	}
}
